/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.store.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.javaeye.common.util.ListItem;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.web.PageInfo;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.dto.MaterialsCategory;
import com.javaeye.common.service.ICodeService;
import com.javaeye.store.service.IMaterialsCategoryService;
import com.javaeye.store.service.IMaterialsService;

public class MaterialsAction extends ActionSupport {
	
	protected static Log log = LogFactory.getLog(MaterialsAction.class); 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3216585574200112708L;

	/**
	 * 用于显示一览结果集
	 */
	private List<Materials> materialsList;
	
	/**
	 * 用于接收分页值对象
	 */
	private PageInfo pageInfo;
	
	/**
	 * 用于接收查询条件值对象
	 */
	private Materials condition;
	
	/**
	 * 用于接收添加画面和编辑画面的值对象
	 */
	private Materials materials;
	
	/**
	 * 用于接收需要修改和删除的ID值
	 */
	private String materialsId;
	
	/**
	 * 用于生成查询画面上选择分类的下拉框的值
	 */
	private List<MaterialsCategory> categoryList;
	
	/**
	 * 用于生成查询画面上选择单位的下拉框的值
	 */
	private List<ListItem> unitsList;
	
	private String rootCategory;
	
	
	/**
	 * 应用autocomplete的查询使用接口
	 */
	private String q;
	
	/**
	 * Service
	 */
	private IMaterialsService service;
	
	private IMaterialsCategoryService categoryService;
	
	private ICodeService codeService;
	
	public void setService(IMaterialsService service) {
		this.service = service;
	}

	public void setCategoryService(IMaterialsCategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	public void setCodeService(ICodeService codeService) {
		this.codeService = codeService;
	}
	
	public MaterialsAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new Materials();
		condition.setName("");
		condition.setCode("");
		condition.setCategory(-1);
		rootCategory = "-1";
	}
	
	public String queryMaterialsNames() throws Exception {
		materialsList = service.getAutoCompleteMaterials(q, 10);
		for (Materials m : materialsList) {
			MaterialsCategory category = categoryService.getCategory(m.getCategory());
			m.setCategoryName(category.getName());
		}
		return SUCCESS;
	}
	
	/**
	 * 条件分页查询
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsList() throws Exception {
		//log.debug("currentPage=" + pageInfo.getPageIndex() + 
		//		", category=" + condition.getCategory() + 
		//		", name=" + condition.getName());
		categoryList = categoryService.getCategorys(0);
		materialsList = service.getMaterialsList(condition, pageInfo);
		for (Materials m : materialsList) {
			m.setCategoryName(categoryService.getCategory(m.getCategory()).getName());
		}
		return SUCCESS;
	}
	
	/**
	 * 保存物料
	 * @return
	 * @throws Exception
	 */
	public String saveMaterials() throws Exception {
		boolean success = service.saveMaterials(materials);
		if (!success) {
			List<Object> params = new ArrayList<Object>();
			params.add("物料");
			this.addActionError(getText("actionErrorSaveUNIQUE", params));
			queryForAddMaterials();
			return INPUT;
		}
		return SUCCESS;
	}
	
	/**
	 * 查询物料
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsForEdit() throws Exception {
		queryForAddMaterials();
		materials = service.getMaterials(materialsId);
		materials.setCategoryName(categoryService.getCategory(
				materials.getCategory()).getName());
		rootCategory = String.valueOf(categoryService.getCategory(
						materials.getCategory()).getParentId());
		return SUCCESS;
	}
	
	/**
	 * 查询用于添加物料
	 * @return
	 * @throws Exception
	 */
	public String queryForAddMaterials() throws Exception {
		categoryList = categoryService.getCategorys(0);
		unitsList = ListUtil.unitList(codeService);
		return SUCCESS;
	}
	
	/**
	 * 删除物料
	 * @return
	 * @throws Exception
	 */
	public String deleteMaterials() throws Exception {
		boolean result = service.removeMaterials(materialsId);
		if (!result) {
			List<Object> params = new ArrayList<Object>();
			params.add("物料");
			this.addActionError(getText("actionErrorDeleteFK", params));
			queryMaterialsList();
			return INPUT;
		}
		return SUCCESS;
	}

	public List<Materials> getMaterialsList() {
		return materialsList;
	}

	public void setMaterialsList(List<Materials> materialsList) {
		this.materialsList = materialsList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public Materials getCondition() {
		return condition;
	}

	public void setCondition(Materials condition) {
		this.condition = condition;
	}

	public Materials getMaterials() {
		return materials;
	}

	public void setMaterials(Materials materials) {
		this.materials = materials;
	}

	public String getMaterialsId() {
		return materialsId;
	}

	public void setMaterialsId(String materialsId) {
		this.materialsId = materialsId;
	}

	public List<MaterialsCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<MaterialsCategory> categoryList) {
		this.categoryList = categoryList;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public void setUnitsList(List<ListItem> unitsList) {
		this.unitsList = unitsList;
	}

	public List<ListItem> getUnitsList() {
		return unitsList;
	}

	public void setRootCategory(String rootCategory) {
		this.rootCategory = rootCategory;
	}

	public String getRootCategory() {
		return rootCategory;
	}

}
